TST/ENH: raise TypeError in Series.searchsorted for incomparable obje… #63336
+28
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…ct-dtype values; add test
doc/source/whatsnew/vX.X.X.rstfile if fixing a bug or adding a new feature.AGENTS.md.Summary
This small change makes Series.searchsorted raise a TypeError when the underlying values are a numpy ndarray with dtype=object containing elements that are not mutually comparable with the provided search value (for example, mixing int and str). This aligns the behavior of searchsorted with sort_values and reduces surprising cases where NumPy's searchsorted can return an index even though comparisons between the types would fail.
Files changed
pandas/core/base.py
Add a lightweight runtime comparability check for object-dtype ndarrays in IndexOpsMixin.searchsorted. If a simple sample comparison between an array element and the search value raises TypeError, we propagate that TypeError.
pandas/tests/series/methods/test_searchsorted.py
Add test_searchsorted_incomparable_object_raises which asserts that Series([1, 2, "1"]).searchsorted("1") raises TypeError.
Rationale
Pandas delegates searchsorted to NumPy for ndarray-backed data. NumPy's behavior on mixed-type object arrays can be surprising: it sometimes finds an insertion index even when Python comparisons between element types would raise TypeError (e.g. 1 < "1"). Other pandas operations (like sort_values) raise in that situation, so this change makes searchsorted consistent with the rest of pandas.
Behavior and trade-offs
The comparability check is deliberately lightweight: it attempts a single comparison between the first non-NA array element and the sample search value. If that raises TypeError, we re-raise.
This heuristic catches the common case (mixed ints/strings) without scanning the whole array (which would be expensive). It may not detect all pathological mixed-type arrays (for example, if the first element is comparable but later ones are not). If we want a stricter rule we can instead sample more elements or check types across the array, at some performance cost.
Testing